package com.sdp.wz.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.cache.WzCleanCache;
import com.sdp.wz.db.wz_vendor_info.entity.WzVendorInfo;
import com.sdp.wz.db.wz_vendor_info.service.IWzVendorInfoService;
import com.sdp.wz.utils.WzConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzVendorInfoController")
public class WzVendorInfoController {

	@Autowired
	private IWzVendorInfoService iWzVendorInfoService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private WzCleanCache wzCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWzVendorInfoService, WzVendorInfo.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WzVendorInfo vendorInfo) {
		if(vendorInfo==null) {
			return Result.error("供应商信息为空");
		}
		if(StringUtils.isEmpty(vendorInfo.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzVendorInfoService, WzVendorInfo.class, new String[] {"vendorCode","vendorName"}, "keyId", vendorInfo, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("供应商编号和名称不能重复");
			}
			vendorInfo.setIsUse(WzConstants.是.getCode());
			vendorInfo.setCreateTime(LocalDateTime.now());
			iWzVendorInfoService.save(vendorInfo);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzVendorInfoService, WzVendorInfo.class, new String[] {"vendorCode","vendorName"}, "keyId", null, vendorInfo, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("供应商编号和名称不能重复");
			}
			
			iWzVendorInfoService.updateById(vendorInfo);
		}
		wzCleanCache.cleanVendorCache();
		return Result.ok();
	}
}

